package com.lxh.libmanager.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxh.libmanager.dao.BookDao;
import com.lxh.libmanager.entity.Book;
import com.lxh.libmanager.services.BookServices;
import com.lxh.libmanager.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BookServicesImpl implements BookServices {
    @Autowired
    private BookDao bookDao;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<Book> getBookByPage(Map<String, String> books) {
        System.out.println("分页查询");

        int num = StringUtil.stringToInt(books.get("pageNum"));
        System.out.println(num);

        System.out.println(StringUtil.stringToInt(books.get("pageSize")));

        PageHelper.startPage(num, StringUtil.stringToInt(books.get("pageSize")));
        List<Book> list = bookDao.getAllBook();
        PageInfo<Book> info = new PageInfo<>(list);
        return info;
    }



    @Override
    public boolean addBook(Book book) {
        if(bookDao.addBook(book)>0){
            return true;
        }
        return false;
    }

    @Override
    public Book getBookById(Integer id) {
        if(id>0){
            return bookDao.getBookById(id);
        }
        return null;
    }

    @Override
    public boolean updateBook(Book book) {
        if(bookDao.updateBook(book)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delBook(Integer id) {
        if(bookDao.delBook(id)>0){
            return true;
        }
        return false;
    }
}
